package order_service

import (
	"github.com/sirupsen/logrus"
	"gorm.io/gorm"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
)

func (o OrderService) OrderSuccess(no string) (err error) {
	//走用户购买成功的逻辑
	var order models.OrderModel
	err = global.DB.Take(&order, "no=?", no).Error
	if err != nil {
		logrus.Error("订单不存在")
		return err
	}
	err = o.userservice.UserScope(global.DB, order.UserID, int(order.Scope))
	if err != nil {
		logrus.Error(err)
		return err
	}
	// 更新用户的金额
	global.DB.Model(&models.UserModel{}).Where("id = ?", order.UserID).Update("money", gorm.Expr("money + ?", order.Price))
	logrus.Infof("更新用户金额 %d  %d", order.UserID, order.Price)
	//更新用户的支付状态
	global.DB.Model(&order).Update("pay_status", ctype.PaidPayStatus)
	global.DB.Model(&order).Update("payed_time", order.UpdatedAt)

	return nil
}
